package com.haoran.dwz.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.haoran.dwz.domain.pojo.PermissionVO4Add;
import com.haoran.dwz.domain.pojo.PermissionVO4Update;
import com.haoran.dwz.rest.R;
import com.haoran.dwz.service.PermissionService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping(value = "/permissions", headers = "Authorization", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(tags = { "Permissions" }, description = "权限管理",position=8)
public class PermissionController {
	@Autowired
	private PermissionService permissionService;

	@ApiOperation("查询所有权限列表")
	@RequestMapping(method = RequestMethod.GET)
	@ApiImplicitParams({
			@ApiImplicitParam(dataType = "String", paramType = "header", required=true, name = "Authorization", value = "登录成功返回的token") })
	public R list() {
		try {
			return R.success(permissionService.treePermissions());
		} catch (Exception e) {
			return R.failure(e);
		}
	}
	
	@ApiOperation("新增权限项")
	@RequestMapping(method = RequestMethod.POST)
	@ApiImplicitParams({
			@ApiImplicitParam(dataType = "String", paramType = "header", required=true, name = "Authorization", value = "登录成功返回的token") })
	public R add(@RequestBody PermissionVO4Add permissionVO) {
		try {
			return R.success(permissionService.newAdd(permissionVO));
		} catch (Exception e) {
			return R.failure(e);
		}
	}

	@ApiOperation("编辑权限项")
	@RequestMapping(method = RequestMethod.PUT)
	@ApiImplicitParams({
			@ApiImplicitParam(dataType = "String", paramType = "header", required=true, name = "Authorization", value = "登录成功返回的token") })
	public R update(@RequestBody PermissionVO4Update permissionVO4Update) {
		try {
			return R.success(permissionService.update(permissionVO4Update));
		} catch (Exception e) {
			return R.failure(e);
		}
	}
	@ApiOperation("删除权限项")
	@RequestMapping(path="{id}",method = RequestMethod.DELETE)
	@ApiImplicitParams({
			@ApiImplicitParam(dataType = "String", paramType = "header", required=true, name = "Authorization", value = "登录成功返回的token"),
			})
	public R delete(@PathVariable String id,@ApiParam("是否包含下级，默认不包含")@RequestParam(defaultValue="false",required=false) boolean subInclude) {
		try {
			permissionService.delete(id,subInclude);
			return R.success("删除成功");
		} catch (Exception e) {
			return R.failure(e);
		}
	}
}
